package com.pdstudio.video;

import java.io.IOException;

import com.pdstudio.weather.R;

import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnInfoListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.media.MediaPlayer.OnSeekCompleteListener;
import android.media.MediaPlayer.OnVideoSizeChangedListener;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;

public class TestVideoActivity extends Activity implements
		OnCompletionListener, OnErrorListener, OnInfoListener,
		OnPreparedListener, OnSeekCompleteListener, OnVideoSizeChangedListener,
		SurfaceHolder.Callback {

	private Display currentDisplay = null;
	private SurfaceView surfaceView = null;
	private SurfaceHolder surfaceHoler = null;
	private MediaPlayer mPlayer = null;
	private int videoWidth = 0, videoHeight = 0;
	private boolean readyToPlay = false;
	private static final String TAG = "-----VideoPlayer----";

	private String address;

	private MediaPlayer media = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);// 隐藏标题
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);// 设置全屏
		setContentView(R.layout.test_video);
		// SurfaceView设置
		surfaceView = (SurfaceView) findViewById(R.id.test_videoSurfaceView);
		surfaceHoler = surfaceView.getHolder();
		surfaceHoler.addCallback(this);
		surfaceHoler.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
		// MediaPlayer设置
		mPlayer = new MediaPlayer();
		mPlayer.setOnCompletionListener(this);
		mPlayer.setOnErrorListener(this);
		mPlayer.setOnInfoListener(this);
		mPlayer.setOnPreparedListener(this);
		mPlayer.setOnSeekCompleteListener(this);
		mPlayer.setOnVideoSizeChangedListener(this);
		// 播放歌曲路径
		// Intent intent = getIntent();
		// address=intent.getStringExtra("ADDRESS");
		address = "http://113.247.234.237/uploads/Files/000/000/001/108/-18235266093785.mp4";
		if (address == null || "".equals(address))
			finish();
		// DebugUtil.printDebugTag("学习园地", "播放地址："+address);
		try {
			mPlayer.setDataSource(address);
		} catch (IOException e) {
			e.printStackTrace();
			// finish();
		}
		currentDisplay = getWindowManager().getDefaultDisplay();
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
	}

	// create方法进行准备
	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		Log.v(TAG, "surfaceCreated method is called!!");
		mPlayer.setDisplay(holder);
		try {
			mPlayer.prepare();
		} catch (IOException e) {
			e.printStackTrace();
			finish();
		}
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
	}

	@Override
	public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
	}

	@Override
	public void onSeekComplete(MediaPlayer mp) {
	}

	// 播放处理
	@Override
	public void onPrepared(MediaPlayer mp) {
		// 全屏播放
		videoHeight = currentDisplay.getHeight();
		videoWidth = currentDisplay.getWidth();
		// 按视频本身大小播放
		// videoWidth = mPlayer.getVideoWidth();
		// videoHeight = mPlayer.getVideoHeight();
		// 在此可以根据情况进行缩放播放设置
		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
				videoWidth, videoHeight);
		// 居中
		lp.gravity = Gravity.CENTER_HORIZONTAL;
		surfaceView.setLayoutParams(lp);
		mPlayer.start();
	}

	// Info 信息处理
	@Override
	public boolean onInfo(MediaPlayer mp, int what, int extra) {
		Log.v(TAG, "onInfo method is called!!");
		if (what == MediaPlayer.MEDIA_INFO_BAD_INTERLEAVING) {
			Log.v(TAG, "Media Info, Media Info Bad Interleaving " + extra);
		} else if (what == MediaPlayer.MEDIA_INFO_NOT_SEEKABLE) {
			Log.v(TAG, "Media Info, Media Info Not Seekable " + extra);
		} else if (what == MediaPlayer.MEDIA_INFO_UNKNOWN) {
			Log.v(TAG, "Media Info, Media Info Unknown " + extra);
		} else if (what == MediaPlayer.MEDIA_INFO_VIDEO_TRACK_LAGGING) {
			Log.v(TAG, "MediaInfo, Media Info Video Track Lagging " + extra);
		} else if (what == MediaPlayer.MEDIA_INFO_METADATA_UPDATE) {
			Log.v(TAG, "MediaInfo, Media Info Metadata Update " + extra);
		}
		return false;
	}

	// Error信息处理
	@Override
	public boolean onError(MediaPlayer mp, int what, int extra) {
		Log.v(TAG, "onError method is called!!");
		if (what == MediaPlayer.MEDIA_ERROR_SERVER_DIED) {
			Log.v(TAG, "Media Error,Server Died" + extra);
		} else if (what == MediaPlayer.MEDIA_ERROR_UNKNOWN) {
			Log.v(TAG, "Media Error,Error Unknown" + extra);
		}
		return false;
	}

	// 播放完毕后,finish
	@Override
	public void onCompletion(MediaPlayer mp) {
		Log.v(TAG, "onCompletion method is called!!");
		finish();
	}

	// Activty销毁释放资源
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		mPlayer.release();
		mPlayer = null;
	}

}
